package com.toune.blackmusic.activity;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.miguelcatalan.materialsearchview.MaterialSearchView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.toune.blackmusic.R;
import com.toune.blackmusic.adapter.AdapterMusicVo;
import com.toune.blackmusic.database.MusicVo;
import com.toune.blackmusic.util.Api;
import com.toune.blackmusic.util.MyUtil;
import com.toune.blackmusic.util.NoHttpCallBack;
import com.toune.blackmusic.util.RequstUtils;
import com.vondear.rxtool.view.RxToast;

import java.util.ArrayList;
import java.util.List;


public class NetMusicActivity extends BaseActivity {

    MaterialSearchView searchView;
    private String webSite = "qq";
    private int page = 1;
    private SmartRefreshLayout refreshLayout;
    private String searchText;
    private RecyclerView mLreclView;
    private AdapterMusicVo adapterMusicVo;

    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_net_music);
        toolbar = findViewById(R.id.toolbar);
        mLreclView = findViewById(R.id.mLreclView);
        refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                searchMusic();
                refreshLayout.finishRefresh();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                searchMusic();
                refreshLayout.autoLoadMore();
            }
        });

        searchView = (MaterialSearchView) findViewById(R.id.search_view);
        searchView.setHint("搜索...");
        searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //Do some magic
                searchText = query;
                searchMusic();
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                //Do some magic
                return false;
            }
        });

        searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
            @Override
            public void onSearchViewShown() {
                //Do some magic
            }

            @Override
            public void onSearchViewClosed() {
                //Do some magic
            }
        });
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.action_search, menu);

        MenuItem item = menu.findItem(R.id.action_search);
        searchView.setMenuItem(item);

        return true;
    }

    private void searchMusic() {
        NoHttpCallBack.params.put("input", searchText);
        NoHttpCallBack.params.put("filter", "name");
        NoHttpCallBack.params.put("type", webSite);
        NoHttpCallBack.params.put("page", page);
        RequstUtils.POSTMUSIC(Api.SearchMusicUrl, new NoHttpCallBack<MusicVo>(MusicVo.class) {
            @Override
            protected void getSucceed(MusicVo musicVo) {
                if (musicVo != null) {
                    if (page == 1) {
                        musicAll.clear();
                    }
                    notifyMusicDataAdapter(musicVo);
                }
            }

            @Override
            protected void getFailed(String json) {
                RxToast.error("ㄟ( ▔, ▔ )ㄏ 网络不好请多搜索几次\"");
            }
        });
    }

    List<MusicVo.DataBean> musicAll = new ArrayList<>();

    private void notifyMusicDataAdapter(MusicVo musicVo) {
        if (musicVo.getData() != null) {
            musicAll.addAll(musicVo.getData());
        }
        if (adapterMusicVo == null) {
            adapterMusicVo = new AdapterMusicVo(R.layout.adapter_music, musicAll);
            mLreclView.setLayoutManager(new LinearLayoutManager(context));
            mLreclView.setAdapter(adapterMusicVo);
        } else {
            adapterMusicVo.notifyDataSetChanged();
        }
    }
}
